/*
 * 创建日期 2005-11-29
 *
 */
package wangjg.commons.io.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/**
 * @author wangjg
 *
 */
public class FileIOHelper {

	/**
	 * 缓存长度
	 */
	static final int bufLength = 512;

	/**
	 * copy file 复制文件
	 * @param source 源文件对象
	 * @param target 目标文件对象
	 * @throws IOException 文件不存在或读写错误时抛出异常
	 */
	public static void FileToFile(File source,File target){
		try {
			InputStream is = new FileInputStream(source);
			OutputStream os = new FileOutputStream(target);
			transmitAndClose(is, os);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 写入文件
	 * @param filepath 文件路径
	 * @param os 输出流
	 * @throws IOException 当读取，写入数据错误是抛出异常
	 */
	public static void FileToStream(String filepath,OutputStream os) throws IOException{
		FileToStream(new File(filepath),os);
	}	
	/**
	 * 写入文件
	 * @param file 文件对象
	 * @param os 输出流
	 * @throws IOException 当读取，写入数据错误是抛出异常
	 */
	public static void FileToStream(File file,OutputStream os) throws IOException{
		InputStream is = null;
		try{
			is = new FileInputStream(file);
			transmit(is,os);
		}finally{
			if(is!=null){
				is.close();
			}	
		}
	}
	
	/**
	 * 写入文件
	 * @param is 输入流
	 * @param file 文件对象
	 * @throws IOException 当读取，写入数据错误是抛出异常
	 */
	public static void StreamToFile(InputStream is,File file) throws IOException{
		OutputStream os = null;
		try{
			os = new FileOutputStream(file);
			transmit(is,os);
		}finally{
			if(os!=null){
				os.close();
			}
		}
	}
	/**
	 * 写入文件
	 * @param is 输入流
	 * @param filename 文件名
	 * @throws IOException 当读取，写入数据错误是抛出异常
	 */
	public static void StreamToFile(InputStream is,String filename) throws IOException{
		StreamToFile(is, new File(filename));
	}
	
	/**
	 * 流到流拷贝数据
	 * @param is 输入流
	 * @param os 输出流
	 * @throws IOException 读写异常
	 */
	public static void StreamToStream(InputStream is,OutputStream os) throws IOException{
		transmit(is, os);
	}
	/**
	 * 在流之间拷贝数据
	 * @param is 输入流
	 * @param os 输出流
	 * @throws IOException 读写异常
	 */
	static void transmit(InputStream is,OutputStream os) throws IOException{
		byte[] buf = new byte[bufLength];
		int count = is.read(buf); 
		while(count!=-1){ 
			os.write(buf,0,count); 
			count = is.read(buf);
		}
	}
	/**
	 * 把字符串写入文件
	 * @param str 字符串
	 * @param filepath 文件路径
	 * @throws IOException 读写异常
	 */
	public static void StringToFile(String str,String filepath) throws IOException{
		FileWriter writer=new FileWriter(filepath);
		writer.write(str);
		writer.close();
	} 
	/**
	 * 把文件内容以字符的形式全部读取
	 * @param filepath 文件路径
	 * @param charset 字符集
	 * @return 字符串
	 */
	public static String FileToString(String filepath,String charset){
		FileInputStream fis=null;
		try{
			fis = new FileInputStream(filepath);
			return StreamToString(fis,charset);
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				if(fis!=null)fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static String FileToString(File file, String charset){
		FileInputStream fis=null;
		try{
			fis = new FileInputStream(file);
			return StreamToString(fis,charset);
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try {
				if(fis!=null)fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 把InpuStream内容以字符的形式全部读取
	 * @param is 输入流
	 * @param charset 字符集
	 * @return 字符串
	 * @throws IOException
	 */
	public static String StreamToString(InputStream is,String charset) throws IOException{
		InputStreamReader sr = new InputStreamReader(is,charset);
		StringBuilder sb = new StringBuilder();
		char[] cbuf = new char[512];
		int n=0;
		while((n=sr.read(cbuf))!=-1){
			sb.append(cbuf,0 , n);
		}
		sr.close();
		return sb.toString();
	}
	
	static void transmitAndClose(InputStream is, OutputStream os){
		try {
			transmit(is, os);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	static void transmit(Reader reader,Writer writer) throws IOException{
		char[] cbuf=new char[1024];
		int len=-1;
		while((len=reader.read(cbuf))!=-1){
			writer.write(cbuf, 0, len);
		}
	}
	
	public static void main(String[] args) throws IOException {
		FileIOHelper.FileToFile(new File("E:\\facebook\\logs\\log4j.log"), new File("E:\\facebook\\logs\\log4j_tail.log"));
	}
}
